package com.mytool.util;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import java.io.IOException;
import java.util.Map;

/**
 * Freemarker模板引擎服务
 */
@Service
public class TemplateEngineService {

    @Autowired
    private Configuration freemarkerConfig;
    
    /**
     * 渲染模板
     * 
     * @param templateName 模板名称
     * @param dataModel 数据模型
     * @return 渲染后的内容
     * @throws IOException 
     * @throws TemplateException
     */
    public String processTemplate(String templateName, Map<String, Object> dataModel) throws IOException, TemplateException {
        Template template = freemarkerConfig.getTemplate(templateName);
        return FreeMarkerTemplateUtils.processTemplateIntoString(template, dataModel);
    }
} 